package com.centanet.framework.base;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.centanet.centalib.R;
import com.centanet.framework.interfaces.IFragmentPrepared;
import com.centanet.framework.interfaces.ISnack;
import com.centanet.framework.widget.CProgressDialog;
import com.trello.rxlifecycle.components.support.RxFragment;

/**
 * 基类Fragment
 */
public abstract class AbstractFragment extends RxFragment {

    protected IFragmentPrepared iFragmentPrepared;
    protected ISnack iSnack;
    protected boolean isPrepared;//标志初始化是否完成
    protected boolean isVisible;//页面可见
    protected CProgressDialog loadingDialog;//加载进度框

    public void setISnack(ISnack iSnack) {
        this.iSnack = iSnack;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInvisible();
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(setLayoutId(), container, false);
        findViews(view);
        initView();
        isPrepared = true;
        lazyLoad();
        return view;
    }

    /**
     * 初始化view前的内容
     */
    protected void preInit(){

    }

    /**
     * 设置布局Id
     */
    @LayoutRes
    protected abstract int setLayoutId();

    /**
     * findViewById
     *
     * @param view View
     */
    protected abstract void findViews(View view);

    /**
     * view初始化
     */
    protected abstract void initView();

    /**
     * fragment可见
     */
    protected void onVisible() {
        lazyLoad();
    }

    /**
     * fragment不可见
     */
    protected void onInvisible() {

    }

    protected void lazyLoad() {
        if (!isPrepared || !isVisible) {
            return;
        }
        loadData();
    }

    /**
     * 加载数据
     */
    protected void loadData() {
    }

    /**
     * 网络不可用
     */
    protected void netWorkUnable() {
        snack(getString(R.string.network_unable));
    }

    /**
     * snack
     *
     * @param text 显示内容
     */
    protected void snack(CharSequence text) {
        if (iSnack != null) {
            iSnack.showSnack(text);
        }
    }

    /**
     * Toast统一入口
     *
     * @param text 显示内容
     */
    protected void toast(CharSequence text) {
        Toast.makeText(getActivity().getApplicationContext(), text, Toast.LENGTH_SHORT).show();
    }

    /**
     * 显示进度Dialog
     */
    protected void showLoadingDialog() {
        if (loadingDialog == null) {
            loadingDialog = new CProgressDialog(getContext());
        }
        loadingDialog.show();
    }

    /**
     * 取消进度Dialog
     */
    protected void cancelLoadingDialog() {
        if (loadingDialog != null &&
                loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }
}
